//
//  MovService.m
//  Opper
//
//  Created by Le Dung on 4/16/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "MovService.h"

@interface MovService ()

@end

@implementation MovService
@synthesize isMove;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [StyleClass createNavi:self andTitle:@"Autoverzekering"];
    [StyleClass backgroundInblack:self.view];
    [self styleContaint];
    [self buttonTarget];
    [self loadData];
}

-(void)loadData
{
    NSDate *currDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd-MM-YYYY"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    
    
    self.addressField.placeholder=self.member.address;
    self.postCodeField.placeholder=self.member.postCode;
    self.placeField.placeholder =self.member.place;
    [self.dateButton setTitle:dateString forState:UIControlStateNormal];
}
-(void)acceptPress
{
    //changeAddress:username,passwd,member_id,address,postcode,place,date_move
    
    
    NSString *functionName =@"changeAddress";
    NSMutableDictionary *dictionary =[[[NSMutableDictionary alloc]init] autorelease];
    [dictionary setValue:@"admin" forKey:@"username"];
    [dictionary setValue:@"123456" forKey:@"passwd"];
    [dictionary setValue:self.member.idMember forKey:@"member_id"];

    [dictionary setValue:self.addressField.text forKey:@"address"];
    [dictionary setValue:self.postCodeField.text forKey:@"postcode"];
    [dictionary setValue:self.placeField.text forKey:@"place"];
    [dictionary setValue:self.dateButton.titleLabel.text forKey:@"date_move"];

    NSString *jsonRespones =[GetData sendRequest:functionName andDictionary:dictionary];
    
    NSLog(@"json ======== %@",jsonRespones);
    
    
    Document *page=[[[Document alloc]init]autorelease];
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
}


-(void)cancelPress
{
    [self.navigationController  popViewControllerAnimated:YES];
}

-(void)buttonTarget
{
    [self.acceptButton addTarget:self action:@selector(acceptPress) forControlEvents:UIControlEventTouchUpInside];
    [self.acceptButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelButton setBackgroundImage:[StyleClass nRed] forState:UIControlStateNormal];
    
    [self.dateButton addTarget:self action:@selector(datePress) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.dateButton setBackgroundImage:[StyleClass nList] forState:UIControlStateNormal];
    [self.addressField setBackground:[StyleClass nText]];
    [self.postCodeField setBackground:[StyleClass nText]];
    [self.placeField setBackground:[StyleClass nText]];
    self.addressField.delegate=self;
    self.postCodeField.delegate=self;
    self.placeField.delegate=self;
    
    
    self.datePicker =[[UIDatePicker alloc]initWithFrame:CGRectMake(self.dateButton.frame.origin.x, self.dateButton.frame.origin.y+35, 280, 100)];
    [self.datePicker setDatePickerMode:UIDatePickerModeDate];
    [self.datePicker sizeThatFits:CGSizeMake(100, 100)];
    [self.containView addSubview:self.datePicker];
    [self.datePicker addTarget:self action:@selector(dateSelect:) forControlEvents:UIControlEventValueChanged];
    
    CGRect pickerFrame=self.datePicker.frame;
    pickerFrame.size.width=280;
    pickerFrame.size.height=50;
    self.datePicker.frame=pickerFrame;
    [self.datePicker setHidden:YES];
    [self.datePicker release];

}

-(void)dateSelect:(UIDatePicker*)datePicker
{
    
    NSDate *currDate = [datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd-MM-YYYY"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    [self.dateButton setTitle:dateString forState:UIControlStateNormal];
}


-(void)datePress
{
    self.datePicker.hidden ?[self.datePicker setHidden:NO ]:[self.datePicker setHidden:YES];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder ];
    
    if(isMove)
    {
    [StyleClass moveDown:self.containView andYLocation:100];
        isMove=0;
    }
    return YES;
}


-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (!isMove)
    {
    [StyleClass moveUp:self.containView andYLocation:100];
        isMove=1;
    }
    
}
-(void)styleContaint
{
    [StyleClass background:self.containView];
    self.containView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.containView.layer.shadowOffset = CGSizeMake(0.0, 2.0);
    self.containView.layer.shadowOpacity = 1.0f;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_containView release];
    [_acceptButton release];
    [_cancelButton release];
    [_addressField release];
    [_postCodeField release];
    [_placeField release];
    [_dateButton release];
    [super dealloc];
}
@end
